<template>
  <div class="login_container">
    <div class="login_box">
      <el-form ref="loginFormRef" label-width="0" class="login_form" :model="loginForm" :rules="loginRules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在3-6个字符", trigger: "blur" }
        ],
        password: [
					{ required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在6-15个字符", trigger: "blur" }
				]
      }
    };
	},
	methods:{
		resetLoginForm(){
			this.$refs.loginFormRef.resetFields()
		},
		login(){
			this.$refs.loginFormRef.validate(async (valid)=>{
				if(!valid) return;
				let { data:res } = await this.$axios.post('login',this.loginForm);
				console.log(res.data);
				if(res.meta.status !==200) {
					return this.$message({
						type:'error',
						message:'登陆失败!'
					});
				}else{
					this.$message({
						type:'success',
						message:'登陆成功!'
					});
					// tocken只能在当前网站大开期间生效，所以讲tocken存到sessionStorage中
					window.sessionStorage.setItem('token',res.data.token);
					this.$router.push('/home')
				}
				
			})
		}
	}
};
</script>

<style lang="less" scoped>
@c: #2b4b6b;
.login_container {
  background: @c;
  width: 100%;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 220px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>